﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Blog.Common.Helper
{
    public class StringHelper
    {
        private const char UNDERLINE = '_';
        /// <summary>
        /// 驼峰转下划线
        /// </summary>
        /// <returns></returns>
        public static string CamelToUnderline(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return "";
            }
            var regex = new Regex("[A-Z]");
            var result = regex.Replace(str, match =>
            {
                if (match.Index == 0)
                {
                    return match.Value.ToLower();
                }
                return UNDERLINE + match.Value.ToLower();
            });
            return result;
        }
    }
}
